// pages/myCoupons/myCoupons.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userCouponsId:[],//用户拥有的优惠券的id列表
    couponsList:[],
    type: 1,
    price:0,
    dianji:"dianji",
    time:new Date()
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //获取手机高度
    this.setData({
      navH: app.globalData.navHeight,
    })
    var productList = JSON.parse(options.productList);
    var productIdList = [];
    var productNumList = [];
    var productPriceList = [];
    for(var i=0;i<productList.length;i++){
      var totalPrice = productList[i].num*productList[i].price;
      productIdList[i] = "{productId:" + productList[i].productId+",totalPrice:"+totalPrice+"}"
      productNumList[i] = productList[i].num
      productPriceList[i] = productList[i].price
    }
    var str = "[";
    var str1 = "";
    var str2 = "";
    for (var i = 0; i < productIdList.length; i++) {
      str += productIdList[i] + ",";
      str1 += productNumList[i]+",";
      str2 += productPriceList[i]+",";
    }
    str = str.substring(0, str.length - 1);
    str1 = str1.substring(0, str1.length - 1);
    str2 = str2.substring(0, str2.length - 1);
    str+="]";
    console.log(str)
    this.setData({
      navH: app.globalData.navHeight,
      productIdList:str,
      productNumList:str1,
      productPriceList:str2
    })
    this.setData({
      price:options.price
    });
    var that = this;
    this.getAllCoupons();
  },

  // 点击tab切换view
  city: function (e) {
    var userid = wx.getStorageSync("userId");
    var that = this;
    if (e.target.dataset.type == 1){
      that.setData({
        dianji: "dianji"
      })
    }else{
      that.setData({
        dianji: ""
      })
    }
    if (this.data.type === e.target.dataset.type) {
      return false;
    } else {
      that.setData({
        type: e.target.dataset.type,
      })
    }
    wx.request({
      url: app.globalData.url + 'userAvailableAndUnavailableCoupon',
      method: 'GET',
      data: {
        userId: wx.getStorageSync("userId"),
        // type: that.data.type,
        // threshold: that.data.price,
        productIdList: that.data.productIdList,
        // productNumList: that.data.productNumList,
        // productPriceList: that.data.productPriceList
      },
      success: function (res) {
        
        that.setData({
          couponsList: res.data.data
        });
      },
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    wx.request({
      url: app.globalData.url + 'userAvailableAndUnavailableCoupon',
      method: 'GET',
      data: {
        userId: wx.getStorageSync("userId"),
        type: that.data.type,
        threshold: that.data.price,
        productIdList: that.data.productIdList,
        productNumList: that.data.productNumList,
        productPriceList: that.data.productPriceList
      },
      success: function (res) {
        that.setData({
          couponsList: res.data.data
        });
      },
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  /**
   * 获取所有可用的优惠券
   */
  getAllCoupons: function () {
    var that = this;

    // wx.request({
    //   url: app.globalData.url + 'getUserConponListByUserId',
    //   data:{
    //     userId: wx.getStorageSync("userId"),
    //     type: that.data.type,
    //     threshold: that.data.price,
    //     productIdList: that.data.productIdList,
    //     productNumList: that.data.productNumList,
    //     productPriceList: that.data.productPriceList
    //   },
    //   method: 'GET',
    //   success: function(res){
    //     that.setData({
    //       couponsList: res.data.data
    //     });
    //   },
    // });
  },

  getUserCouponsId: function () {
    var that = this;
    wx.request({
      url: app.globalData.url + 'getUserCouponsList?userId=1&type=0',
      method: 'GET',
      success: function (res) {
        that.setData({
          userCouponsId: res.data.data
        });
      },
    });
  },
  dianji: function (e) {
    let pages = getCurrentPages(); //获取当前页面js里面的pages里的所有信息。

    let prevPage = pages[pages.length - 2];
    prevPage.setData({  // 将我们想要传递的参数在这里直接setData。上个页面就会执行这里的操作。
      reducePrice: e.currentTarget.dataset.price,
      ucId: e.currentTarget.dataset.ucid,
      typedianji: e.currentTarget.dataset.typedj,
      couponId: e.currentTarget.dataset.couponid
    })
    this.setData({
      reducePrice1: e.currentTarget.dataset.price,
    })
    wx.navigateBack({
      data: 1
    })
  },
  navBack: function () {
    wx.navigateBack({
      data: 1
    })
  }
})